// pagesShangping/pages/addAddress/index.js
const app = getApp();
import { baseUrl } from "../../../api/request";
import { getStorage } from "../../../utils/localStorage.js";
import { address_Add, address_Upload, address_Info } from "../../../api/shopping"
Page({

    /**
     * 页面的初始数据
     */
    data: {
        //自定义导航栏参数
        navData: {
            title: '添加收获地址',
            level: 2 //导航栏级别  
        },
        pageTop: app.globalData.navHeight + app.globalData.navTop,
        buttonState: false,
        form: {},
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.reset();
        if (JSON.stringify(options) !== "{}") {
            let title = undefined;
            if (options.type === 'upload') {
                title = '修改收货人信息';
                this.getInfo(options.id);
            } else {
                title = '添加收货人信息';
                this.reset();
            };
            this.setData({ 'navData.title': title });
        } else {
            wx.showToast({
                icon: 'none',
                title: '页面参数获取失败',
                duration: 2000
            })
        };
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    //重置表单数据
    reset() {
        this.setData({
            form: {
                userId: getStorage('user').id,
                receiver: undefined, //收货人
                phone: undefined, //手机号码
                province: undefined, //省份
                city: undefined, //城市
                county: undefined, //县市区
                addr: undefined, //详细地址
                isDefault: '1', //县市区
                address: undefined,//收货地址
            }
        });
    },
    //查询地址详情
    getInfo(id) {
        this.reset();
        address_Info({ id: id }).then(res => {
            this.setData({
                "form.id": res.data.data.id,
                "form.address": res.data.data.province + res.data.data.city + res.data.data.county,
                "form.receiver": res.data.data.receiver,
                "form.phone": res.data.data.phone,
                "form.province": res.data.data.province,
                "form.city": res.data.data.city,
                "form.county": res.data.data.county,
                "form.isDefault": JSON.stringify(res.data.data.isDefault),
                "form.addr": res.data.data.addr,
            });
        })
    },
    //收货人input框bindinput事件
    receiverInput(e) {
        this.setData({ "form.receiver": e.detail.value });
    },
    //手机号码input框bindinput事件
    phoneInput(e) {
        this.setData({ "form.phone": e.detail.value });
    },
    //所在地址picker change事件
    addressChange(e) {
        const arr = e.detail.value;
        this.setData({
            "form.province": arr[0],
            "form.city": arr[1],
            "form.county": arr[2],
            "form.address": arr.toString()
        });
    },
    //设为默认地址switch change事件
    isDefaultChange(e) {
        this.setData({ "form.isDefault": e.detail.value ? '1' : '0' });
    },
    //详细地址input框bindinput事件
    addrInput(e) {
        this.setData({ "form.addr": e.detail.value });
    },


    //保存按钮点击事件
    saveTap() {
        const isEnty = Object.values(this.data.form).every(val => val);
        if (isEnty) {
            const addressOpt = !this.data.form.id ? address_Add(this.data.form) : address_Upload(this.data.form);
            addressOpt.then(res => {
                this.setData({ buttonState: true });
                wx.showToast({
                    icon: 'none',
                    title: res.data.msg,
                    duration: 2000
                });
                if (res.data.code === 200) {
                    setTimeout(() => {
                        this.setData({
                            buttonState: false
                        });
                        wx.navigateBack();
                    }, 2000);
                };
            });
        } else {
            wx.showToast({
                icon: 'none',
                title: '以上内容都不能为空',
                duration: 2000
            });
        };
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})